Import yimDebug
Import yimMath

#If TARGET = "html5" Or TARGET = "flash" Or TARGET = "glfw" Or TARGET = "xna"
Import "native/yimwork.${TARGET}.${LANG}"

Extern

#If TARGET = "xna"

	Function yim_saveString:Void(url:String, file:String, data:String) = "yimWork.saveString"

#Else

	Function yim_saveString:Void(url:String, file:String, data:String) = "yim_saveString"
	
	Function yimCreateImage:Image(width:Int, height:Int)
	Function yimLockRenderTarget()

	Function yimUnlockRenderTarget()
	
#EndIf

	
Public

#EndIf

Global yim_prevColor:Float[] = New Float[3]
Global yim_prevAlpha:Float = 1.0

Function yimSaveString(file:String, str:String)
	Local url:String = html5_flash_save_url '"http://remotetrans.invaderjim.com/datagetter.php"
	
	yim_saveString(url, file, str)
	
End

Function yim_SaveColor()
	Local pColor:Float[] = GetColor()
	
	yim_prevColor[0] = pColor[0]
	yim_prevColor[1] = pColor[1]
	yim_prevColor[2] = pColor[2]
End

Function yim_RestoreColorAndAlpha()
	yim_RestoreColor()
	yim_RestoreAlpha()
End

Function yim_SaveColorAndAlpha()
	yim_SaveColor()
	yim_SaveAlpha()
End

Function yim_RestoreColor()
	SetColor(yim_prevColor[0], yim_prevColor[1], yim_prevColor[2])
End

Function yim_SaveAlpha()
	yim_prevAlpha = GetAlpha()
End

Function yim_RestoreAlpha()
	SetAlpha(yim_prevAlpha)
End

Function yimArrayDelete(a:String[], index:Int)
	For Local i:= index Until a.Length-1
		a[i] = a[i+1]
	Next
	
	a = a.Resize(a.Length-1)
End

Class yimColor
	Field r:Int
	Field g:Int
	Field b:Int
	Field a:Int
	Field argb:Int
	
	Method New(red:Int, green:Int, blue:Int, alpha:Int=255)
		Set(red, green, blue, alpha)
	End
	
	Method Set(red:Int, green:Int, blue:Int, alpha:Int=255)
		r = red
		g = green
		b = blue
		a = alpha
		SetARGB(r, g, b, a)
	End
	
	Method SetARGB(red:Int, green:Int, blue:Int, alpha:Int=255)
		argb = alpha * $ff000000 + red * $ff0000 + green * $ff00 + blue * $ff
	End
	
	Method GetARGB()
		'a=(argb Shr 24) & $ff
		'r=(argb Shr 16) & $ff
		'g=(argb Shr 8) & $ff
		'b=argb & $ff
		
		Return argb
	End

End

Function DrawArc(x:Float, y:Float, radius:Float, startAng:Float, endAng:Float, angStep:Float=5.0)
	Local lastX:Float = x + Sin(startAng)*radius
	Local lastY:Float = y - Cos(startAng)*radius
	startAng += angStep
	
	While startAng <= endAng
		Local newX:Float = x + Sin(startAng)*radius
		Local newY:Float = y - Cos(startAng)*radius
		DrawLine(lastX, lastY, newX, newY)
		
		lastX = newX
		lastY = newY
		startAng += angStep
	End
		
	DrawLine(lastX, lastY, x + Sin(endAng)*radius, y - Cos(endAng)*radius)
	
End

Function DrawUnfilledCircle(x:Float, y:Float, r:Float, angStep:Float=5.0 )
	DrawArc(x, y, r, 0, 360, angStep)
	
End

Function yimDrawRect(rect:yimRect)
	rect.Draw(True)
End

Function DrawUnfilledRect(rect:yimRect)
	rect.Draw(False)
End

Function DrawUnfilledRect(x:Float, y:Float, w:Float, h:Float)
	DrawLine(x, y, x+w, y)
	DrawLine(x+w, y, x+w, y+h)
	DrawLine(x+w, y+h, x, y+h)
	DrawLine(x, y+h, x, y)
End

Function DrawUnfilledPoly(pts:Float[])
	If pts.Length > 5
		Local px:Float = pts[0]
		Local py:Float = pts[1]
		For Local i:= 2 Until pts.Length Step 2
			DrawLine(px, py, pts[i], pts[i+1])
			px = pts[i]
			py = pts[i+1]
		Next
		DrawLine(px, py, pts[0], pts[1])
	EndIf
End
